package com.tkjt.pms.modules.kpiAudit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.common.entity.TkIndexValueAudit;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.common.vos.RequestJson;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAuditMapper;
import com.tkjt.pms.modules.kpiAudit.model.AuditRecordsResponseModel;
import com.tkjt.pms.modules.kpiAudit.service.TkAuditBizService;
import com.tkjt.pms.modules.kpiAudit.service.TkAuditService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public abstract class TkAuditBizServiceBase implements TkAuditBizService {
	@Autowired
	TkIndexValueAuditMapper tkIndexValueAuditMapper;

	@Override
	public Result getAuditList(HttpServletRequest httpServletRequest, String requestType,String dataType) {
		RequestJson request = EsJsonUtils.getRequestJsonObject(httpServletRequest);
		String indexYear = request.getString("indexYear");
		String indexGroup = request.getString("indexGroup");// 部门类别 C: 一线单位 G：养康本部
		String indexYearMonth = request.getString("indexYearMonth");// 列表信息 接口
		int pageNo = request.getPageNo();
		int pageSize = request.getPageSize();

		QueryWrapper<TkIndexValueAudit> qw = new QueryWrapper<>();
		if (StringUtils.isNotEmpty(indexYear)) {
			qw.eq("INDEX_VALUE_Y", indexYear);
		} else {
			qw.eq("INDEX_VALUE_Y", indexYearMonth.substring(0, 4));
			qw.eq("INDEX_VALUE_M", indexYearMonth.substring(4));
		}
		if (StringUtils.isNotEmpty(indexGroup)) {
			qw.eq("INDEX_GROUP", "C".equals(indexGroup) ? "A01A01A01" : "A01A01A02");
		}
		qw.eq("DATA_TYPE", dataType);
		Page<TkIndexValueAudit> page = new Page<>(pageNo, pageSize);
		IPage<TkIndexValueAudit> auditIPage = tkIndexValueAuditMapper.selectPage(page, qw);
		List<TkIndexValueAudit> auditList = auditIPage.getRecords();
		List<AuditRecordsResponseModel.RecordsBean> records = new ArrayList<>();
		for (TkIndexValueAudit t : auditList) {
			AuditRecordsResponseModel.RecordsBean recordsBean = new AuditRecordsResponseModel.RecordsBean();
			recordsBean.setIndexGroup(t.getIndexGroup());
			recordsBean.setIndexGroupName("A01A01A02".equals(t.getIndexGroup()) ? "养康本部" : "一线单位");
			recordsBean.setAuditStatus(t.getAuditStatus());
			recordsBean.setAuditStatusName(getAuditStatus(t.getAuditStatus()));
			recordsBean.setIndexYear(t.getIndexValueY());
			recordsBean.setIndexMonth(t.getIndexValueM());
			recordsBean.setAuditComments(t.getAuditComments());
			recordsBean.setAuditTime(EsDateUtils.format(t.getAuditTime()));
			recordsBean.setSubmitTime(EsDateUtils.format(t.getSubmitTime()));
			records.add(recordsBean);
		}
		AuditRecordsResponseModel model = new AuditRecordsResponseModel();
		model.setRecords(records);
		model.setCurrent(auditIPage.getCurrent());
		model.setPages(auditIPage.getPages());
		model.setSearchCount(auditIPage.isSearchCount());
		model.setSize(auditIPage.getSize());
		model.setTotal(auditIPage.getTotal());
		return Result.ok(model);
	}

	/**
	 * 审批意见(1:待申请；2:审核中; 3:已审核; 4:已退回)',
	 */
	private String getAuditStatus(String auditStatus) {
		switch (auditStatus) {
		case "0":
			return "已初审";
		case "2":
			return "审核中";
		case "3":
			return "已审核";
		case "4":
			return "已退回";
		default:
			return "待申请";
		}
	}
}
